package com.epfresh.supplier.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.epfresh.supplier.R;

import java.util.List;

/**
 * Created by wzh on 2016/5/6.
 */
public class GridMoreAdapter extends BaseAdapter implements View.OnClickListener {

    public Context context;


    private List<Object> list;
    private AdapterView.OnItemClickListener onItemClickListener;

    private int currentIndex = 0;

    public GridMoreAdapter(Context context, List<Object> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<Object> list){
        if (this.list != null){
            this.list.clear();
            if (list != null)
            this.list.addAll(list);
        }else {
            this.list = list;
        }
    }

    public int getCurrentIndex() {
        return currentIndex;
    }

    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
    }

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_dialog_filter_content, null, false);
            vh.tvContent = (TextView) convertView.findViewById(R.id.tv_dialog_filter_content);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        //Market market = list.get(position);
        vh.tvContent.setText("just try");

        if (currentIndex == position){
            vh.tvContent.setTextColor(context.getResources().getColor(R.color.green));
            vh.tvContent.setEnabled(false);
        }else {
            vh.tvContent.setTextColor(Color.parseColor("#626262"));
            vh.tvContent.setEnabled(true);

        }
        return convertView;
    }


    @Override
    public void onClick(View v) {
        Object o = v.getTag(R.id.item_key_position);
        if (o != null && o instanceof Integer) {
            if (onItemClickListener != null) {
                int position = (int) o;
                onItemClickListener.onItemClick(null, v, position, position);
            }
        }
    }

    public class ViewHolder {
        TextView tvContent;
    }
}
